# Created by 黄景涛
# DATE: 2025/4/14

import translators as ts
import random
from datetime import datetime
from request_task import post_task, get_task
from async_run import async_main


def random_chars(counts, char_type=None):
    char_pools = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.。,"
    if char_type == 'digit':
        char_pools = "".join(c for c in char_pools if c.isdigit())
    elif char_type == 'alpha':
        char_pools = "".join(c for c in char_pools if c.isalpha())
    return ''.join(random.choices(char_pools, k=counts))


def format_time(fmt="%Y-%m-%d_%H%M%S"):
    now_time = datetime.now()
    return now_time.strftime(fmt)


def get_biz_params(i):
    """ 构造params """
    mp = {
        "life_cycle": ["1", "2", "3"],
        "bk_biz_maintainer": ["admin", "product"],
        "operator": ["admin", "product"],
        "time_zone": ["Asia/Shanghai", "Africa/Abidjan", "Africa/Accra"],
        "language": ["1", "2"],
    }

    data = {
        "bk_biz_name": f"0519_业务C{i}",
        "life_cycle": "2",
        "bk_biz_maintainer": "admin",
        "bk_biz_productor": "",
        "bk_biz_tester": "",
        "bk_biz_developer": "",
        "operator": "",
        "time_zone": "Asia/Shanghai",
        "language": "1",
        "bk_created_at": None,
        "bk_created_by": "",
        "bk_updated_at": None,
        "bk_updated_by": ""
    }

    for k, v in data.items():
        if k in mp:
            data[k] = random.choice(mp[k])

    return data


def get_project_params(i):
    mp = {
        "bk_project_sec_lvl": ["public", "private", "classified"],
        "bk_status": ["enable", "disabled"],
        "bk_project_type": ["mobile_game", "pc_game", "web_game", "platform_prod", "support_prod", "other"],
        "bk_project_owner": ["product", "admin"],
        "bk_project_team": [[2], [10], [3]],
    }
    bk_project_name = random.choice(
        ['绿色城市计划', '智能家居系统', '可再生能源推广项目', '数字教育平台', '社区健康促进计划',
         '无人机物流解决方案', '智能交通管理系统', '虚拟现实旅游体验', '企业数字化转型项目',
         '人工智能客服系统'])
    bk_project_code = ts.translate_text(f'{bk_project_name}', from_language='zh', to_language='en')

    data = {
        "data": [
            {
                "id": None,
                "bk_project_code": bk_project_code,
                "bk_project_sec_lvl": "public",
                "bk_status": "enable",
                "bk_project_name": bk_project_name,
                "bk_project_desc": "aws",
                "bk_project_type": "other",
                "bk_project_owner": "product",
                "bk_project_team": [
                    2
                ],
                "bk_created_at": None,
                "bk_created_by": "",
                "bk_updated_at": None,
                "bk_updated_by": ""
            }
        ]
    }

    for k, v in data.items():
        if k in mp:
            data['data'][0][k] = random.choice(mp[k])

    return data


def get_switch_params(i):
    mp = {
        "bk_vendor": ["华为（Huawei）", "思科（Cisco）", "迈普（MikroTik）", "TP-Link"],
        "bk_biz_status": ["1", "2", "3"],
        "bk_admin_ip": ["192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4"],
        "bk_model": [f"{random_chars(5)}-{random_chars(3)}-{random_chars(2)}-{random_chars(2)}", ],
        "bk_sn": [f"{random_chars(3, 'alpha')}{random_chars(9, 'digit')}"]
    }
    asset_prefix = random.choice(['IT', 'OP', 'DEV'])

    data = {
        "bk_asset_id": f"GZ-{asset_prefix}-{i:0>6}",
        "bk_inst_name": f"固资_{asset_prefix}_{i}",
        "bk_sn": "ABC123456789",
        "bk_func": None,
        "bk_vendor": "华为（Huawei）",
        "bk_model": "S5720-32X-SI-AC",
        "bk_admin_ip": "192.168.1.1",
        "bk_operator": None,
        "bk_os_detail": None,
        "bk_detail": None,
        "bk_biz_status": "1",
        "bk_created_at": None,
        "bk_created_by": "",
        "bk_updated_at": None,
        "bk_updated_by": ""
    }

    for k, v in data.items():
        if k in mp:
            data[k] = random.choice(mp[k])

    return data


def get_router_params(i):
    mp = {
        "bk_vendor": ["华为（Huawei）", "思科（Cisco）", "迈普（MikroTik）", "TP-Link"],
        "bk_biz_status": ["1", "2", "3"],
        "bk_admin_ip": ["192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4"],
        "bk_model": [f"{random_chars(5)}-{random_chars(3)}-{random_chars(2)}-{random_chars(2)}", ],
        "bk_sn": [f"{random_chars(3, 'alpha')}{random_chars(9, 'digit')}"]
    }
    data = {
        "bk_asset_id": f"RT-{random_chars(2,'alpha')}{random_chars(3,'digit')}-{i:0>6}",
        "bk_inst_name": f"路由器_{random_chars(2)}_{i}",
        "bk_sn": "ABC123456789",
        "bk_func": "",
        "bk_vendor": "华为（Huawei）",
        "bk_model": "S5720-32X-SI-AC",
        "bk_admin_ip": "192.168.1.1",
        "bk_operator": None,
        "bk_os_detail": "",
        "bk_detail": "",
        "bk_biz_status": "1",
        "bk_created_at": None,
        "bk_created_by": "",
        "bk_updated_at": None,
        "bk_updated_by": ""
    }

    for k, v in data.items():
        if k in mp:
            data[k] = random.choice(mp[k])

    return data


def get_model_1_params(i):
    mp = {
        "singlechar": ["", random_chars(10)],
        "longchar": ["", random_chars(30)],
        "int": [random.randint(1, 999)],
        "float": [random.randint(1, 999) / 2],
        "bool": [True, False],
        "enum": ["chang", "tiao", "rap", "dalaniu"],
        "multi_enum": [[], list(random.choices(["chang1", "tiao1", "rap1", "dalaniu1"], k=random.randrange(1, 5)))],
        "list": ["开发", "测试", "产品", "其他0123456789"],
        "timezone": ["Asia/Shanghai", "Africa/Abidjan", "Africa/Accra"],
        "user": ["", "product", "admin", "product,admin", "product,liyu,libo"],
        "organization": [[], [2], [3], [2, 3]],
        "table": [[]],
        "date": ["2025-04-15", "2025-04-16", "2025-04-17", "2025-04-18"],
        "datetime": ["2025-04-15 05:00:05", "2025-04-16 05:00:05", "2025-04-17 05:00:05", "2025-04-18 05:00:05"],
    }

    data = {
        "bk_inst_name": f"实例_{random_chars(1, 'alpha')}_{i}",
        "bk_created_at": None,
        "bk_created_by": "",
        "bk_updated_at": None,
        "bk_updated_by": "",
        "operator": "product",
        "singlechar": "阿斯顿飞过",
        "longchar": "阿萨德政府规划局",
        "int": 100,
        "float": 0.99,
        "bool": True,
        "enum": "chang",
        "multi_enum": [
            "chang1",
            "tiao1"
        ],
        "list": "测试",
        "timezone": "Asia/Shanghai",
        "user": "liyu,libo",
        "organization": [
            2,
            3
        ],
        "table": [
            {
                "sn": "SQ2025041600000003",
                "sys": [
                    "sys1"
                ],
                "count": 2,
                "commit": "备注一下",
                "is_end": True
            },
            {
                "sn": "SQ2025041600000004",
                "sys": [
                    "sys1",
                    "sys2",
                    "sys3"
                ],
                "count": 1,
                "commit": "安慰虽然",
                "is_end": False
            }
        ],
        "date": "2025-04-16",
        "datetime": "2025-04-16 05:00:05"
    }

    for k, v in data.items():
        if k in mp:
            data[k] = random.choice(mp[k])

    return data


if __name__ == '__main__':
    config = {"thread_count": 10, "task_count": 10}
    # _url = "http://cmdb.azure-bkee5.canwaysoft.com/table/biz/0"
    # _url = "http://cmdb.azure-bkee5.canwaysoft.com/table/create/instance/object/bk_switch"
    # _url = "http://cmdb.azure-bkee5.canwaysoft.com/table/create/instance/object/model_0416_1"
    # _url = "http://cmdb.azure-bkee5.canwaysoft.com/api/v3/createmany/project"
    # _url = "http://cmdb.azure-bkee5.canwaysoft.com/table/create/instance/object/bk_router"

    # _url = "http://cmdb.proj-x86.bkee5.com/table/biz/0"
    # _url = "http://cmdb.proj-x86.bkee5.com/table/create/instance/object/bk_switch"
    # _url = "http://cmdb.proj-x86.bkee5.com/api/v3/createmany/project"

    # _url = "https://cmdb.dev-x86.goldendb5.com/table/biz/0"
    # _url = "https://cmdb.dev-x86.goldendb5.com/table/create/instance/object/bk_switch"
    # _url = "https://cmdb.dev-x86.goldendb5.com/table/create/instance/object/bk_router"
    # _url = "https://cmdb.dev-x86.goldendb5.com/api/v3/createmany/project"

    # _url = "https://cmdb.dev-arm.cwbk5.com/table/biz/0"
    # _url = "https://cmdb.dev-arm.cwbk5.com/table/create/instance/object/bk_switch"
    # _url = "https://cmdb.dev-arm.cwbk5.com/table/create/instance/object/bk_router"
    _url = "https://cmdb.dev-arm.cwbk5.com/api/v3/createmany/project"


    headers = {"Content-Type": "application/json",
               "Cookie": "blueking_language=zh-cn; http_scheme=https; bk_token=bkcrypt%24gAAAAABoKuCsRfpsFo5JswrymF95Iy92etKqct1ExCSjV1iW3hbJc-chJP_fJJRnxUSfTTqjCsiYxLfhvQXwuHaYUPechGUtlY-bfCqBcB3hHWAZ6pzeNk4%3D; cc3=MTc0NzY0MDYyMHxOd3dBTkV4UldUVkJRVFJSVFVwSVRGWkVVRXczVDBOWFRUVk9VazVEV1ZoTU0weE9Va3BTVkZoR1JWRkxXRlpQUjFwSVRrTTNOVkU9fPgilzeHgAVTyGk4u1X2dXdEQhG6QGVcdi9QIop7tDhi"
               }
    async_main(config, post_task, get_project_params, _url, headers)
